package com.zs.games.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.games.resultTool.ResultCodeEnum;
import com.zs.games.entity.UserComment;
import com.zs.games.entity.UserDynamic;
import com.zs.games.entity.UserPraise;
import com.zs.games.enums.SourceTypeEnum;
import com.zs.games.exception.APIException;
import com.zs.games.mapper.UserCommentMapper;
import com.zs.games.mapper.UserDynamicMapper;
import com.zs.games.mapper.UserPraiseMapper;
import com.zs.games.param.PageQuery;
import com.zs.games.param.UserAddDynamicParam;
import com.zs.games.param.UserCommentParam;
import com.zs.games.service.UserDynamicService;
import com.zs.games.utils.JwtTokenUtils;
import com.zs.games.vo.LoginUserVo;
import com.zs.games.vo.UserDynamicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @date 2024年03月19日 14:13
 */
@Service
public class UserDynamicServiceImpl extends ServiceImpl<UserDynamicMapper, UserDynamic> implements UserDynamicService {
    @Autowired
    private UserPraiseMapper userPraiseMapper;
    @Autowired
    private UserCommentMapper userCommentMapper;

    @Override
    public IPage<UserDynamicVo> getPage(PageQuery param) {
        LoginUserVo userInfo = JwtTokenUtils.getUserInfo();
        Page<UserDynamicVo> page = new Page<>(param.getPageNumber(), param.getPageSize());
        IPage<UserDynamicVo> gameNewsPage = baseMapper.findDynamicInfoPage(page, userInfo == null ? null : userInfo.getUserId());
        for (UserDynamicVo dynamic : gameNewsPage.getRecords()) {
            dynamic.setUserCommentList(userCommentMapper.getUserCommentList(dynamic.getDynamicId(), SourceTypeEnum.DYNAMICS.getValue()));
           if (userInfo != null){
               dynamic.setPraiseFlag(userPraiseMapper.selectCount(
                       new QueryWrapper<UserPraise>().lambda().eq(UserPraise::getUserId, userInfo.getUserId()).eq(UserPraise::getSourceId,dynamic.getDynamicId()).eq(UserPraise::getSourceType,SourceTypeEnum.DYNAMICS.getValue())) > 0);
           }
        }
        return gameNewsPage;
    }

    @Override
    public Boolean createDynamic(UserAddDynamicParam param) {
        LoginUserVo userInfoA = JwtTokenUtils.getUserInfoA();
        UserDynamic userDynamic = new UserDynamic(userInfoA.getUserId(), userInfoA.getNickName(), param.getContent(), param.getImages());
        return baseMapper.insert(userDynamic) > 0;
    }

    @Override
    public Boolean deleteDynamic(Integer dynamicId) {
        return baseMapper.deleteById(dynamicId) > 0;
    }

    @Override
    public Boolean praiseDynamic(Integer dynamicId) {
        LambdaQueryWrapper<UserPraise> eq = new QueryWrapper<UserPraise>().lambda()
                .eq(UserPraise::getSourceId, dynamicId)
                .eq(UserPraise::getSourceType, SourceTypeEnum.DYNAMICS.getValue())
                .eq(UserPraise::getUserId, JwtTokenUtils.getUserInfoA().getUserId());
        // 实现点赞逻辑
        int count= userPraiseMapper.selectCount(eq);
        if (count > 0){
            throw new APIException(ResultCodeEnum.ALREADY_EXIST);
        }
        return userPraiseMapper.insert(new UserPraise(JwtTokenUtils.getUserInfoA().getUserId(),dynamicId, SourceTypeEnum.DYNAMICS)) > 0;
    }

    @Override
    public Boolean cancelPraiseDynamic(Integer dynamicId) {
        LambdaQueryWrapper<UserPraise> eq = new QueryWrapper<UserPraise>().lambda()
                .eq(UserPraise::getSourceId, dynamicId)
                .eq(UserPraise::getSourceType, SourceTypeEnum.DYNAMICS.getValue())
                .eq(UserPraise::getUserId, JwtTokenUtils.getUserInfoA().getUserId());
        // 实现点赞逻辑
        int count= userPraiseMapper.selectCount(eq);
        if (count == 0){
            throw new APIException(ResultCodeEnum.ALREADY_EXIST);
        }
        return userPraiseMapper.delete(new QueryWrapper<UserPraise>().lambda()
                .eq(UserPraise::getSourceId, dynamicId)
                .eq(UserPraise::getSourceType, SourceTypeEnum.DYNAMICS.getValue())
                .eq(UserPraise::getUserId, JwtTokenUtils.getUserInfoA().getUserId())) > 0;
    }

    @Override
    public Boolean commentDynamic(UserCommentParam param) {
        UserDynamic userDynamic = baseMapper.selectById(param.getSourceId());
        if (userDynamic == null){
            throw new APIException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        LoginUserVo userInfo = JwtTokenUtils.getUserInfoA();
        if (param.getCommentId() != null){
            UserComment userComment = userCommentMapper.selectById(param.getCommentId());
            if (userComment == null){
                throw new APIException(ResultCodeEnum.COMMENT_DATA_NOT_EXIST);
            }
        }
        UserComment userComment = new UserComment(param.getCommentId(), param.getSourceId(), userInfo.getUserId(), param.getContent(),SourceTypeEnum.DYNAMICS);
        return userCommentMapper.insert(userComment) > 0;
    }

    @Override
    public Boolean deleteCommentDynamic(Integer dynamicId) {
        UserComment userComment = userCommentMapper.selectById(dynamicId);
        if (userComment == null){
            throw new APIException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        if (!userComment.getUserId().equals(JwtTokenUtils.getUserInfoA().getUserId())){
            throw new APIException(ResultCodeEnum.COMMENT_DELETE_ERROR);
        }
        return userCommentMapper.deleteById(dynamicId) > 0;
    }
}
